package com.hopes.bizz.risk.transfer.validation;

import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.risk.transfer.constant.RiskConstant;
import org.apache.commons.lang3.ArrayUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author lyb
 * @version 1.0
 * @description: 包保责任人任务项校验器
 * @date 2024/2/1 15:37
 */
public class HazTaskNumValidator implements ConstraintValidator<HazTaskNum, String> {

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return true;
        }

        String[] taskNumArr = value.split(RiskConstant.VALIDATOR_JOIN);
        String workType = taskNumArr[0];
        if (RiskConstant.RISK_WORK_TYPE_DAILY_TASK.equals(workType)) {
            return true;
        }

        if (taskNumArr.length == 1) {
            return false;
        }

        String taskNum = taskNumArr[1];
        if (StringUtils.isBlank(taskNum)) {
            return false;
        }

        if (RiskConstant.RISK_WORK_TYPE_PRINCIPAL_TASK.equals(workType)) {
            return ArrayUtils.contains(RiskConstant.RISK_WORK_TYPE_PRINCIPAL_TASK_ITEM, taskNum);
        }

        if (RiskConstant.RISK_WORK_TYPE_TECHNICAL_LEAD_TASK.equals(workType)) {
            return ArrayUtils.contains(RiskConstant.RISK_WORK_TYPE_TECHNICAL_LEAD_TASK_ITEM, taskNum);
        }

        if (RiskConstant.RISK_WORK_TYPE_OPERATIONS_MANAGER_TASK.equals(workType)) {
            return ArrayUtils.contains(RiskConstant.RISK_WORK_TYPE_OPERATIONS_MANAGER_TASK_ITEM, taskNum);
        }

        return true;
    }
}
